{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e014d207",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np \n",
    "import pandas as pd\n",
    "\n",
    "from IPython.core.interactiveshell import InteractiveShell # jupyter运行输出的模块\n",
    "\n",
    "#显示每一个运行结果\n",
    "InteractiveShell.ast_node_interactivity = 'all'\n",
    "\n",
    "#设置列不限制数量\n",
    "pd.set_option('display.max_columns', None)\n",
    "\n",
    "#设置行不限制数量\n",
    "#pd.set_option('display.max_rows',None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "350a2335",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 31.2 ms\n",
      "Wall time: 36 ms\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1990-12-19</th>\n",
       "      <td></td>\n",
       "      <td>96.050</td>\n",
       "      <td>99.980</td>\n",
       "      <td>95.790</td>\n",
       "      <td>99.980</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-20</th>\n",
       "      <td>99.98</td>\n",
       "      <td>104.300</td>\n",
       "      <td>104.390</td>\n",
       "      <td>99.980</td>\n",
       "      <td>104.390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-21</th>\n",
       "      <td>104.39</td>\n",
       "      <td>109.070</td>\n",
       "      <td>109.130</td>\n",
       "      <td>103.730</td>\n",
       "      <td>109.130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-24</th>\n",
       "      <td>109.13</td>\n",
       "      <td>113.570</td>\n",
       "      <td>114.550</td>\n",
       "      <td>109.130</td>\n",
       "      <td>114.550</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-25</th>\n",
       "      <td>114.55</td>\n",
       "      <td>120.090</td>\n",
       "      <td>120.250</td>\n",
       "      <td>114.550</td>\n",
       "      <td>120.250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-25</th>\n",
       "      <td>3825.759</td>\n",
       "      <td>3848.163</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3839.972</td>\n",
       "      <td>3883.562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-26</th>\n",
       "      <td>3883.562</td>\n",
       "      <td>3871.471</td>\n",
       "      <td>3888.599</td>\n",
       "      <td>3859.758</td>\n",
       "      <td>3868.382</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-27</th>\n",
       "      <td>3868.382</td>\n",
       "      <td>3869.612</td>\n",
       "      <td>3887.198</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>3800.350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-28</th>\n",
       "      <td>3800.35</td>\n",
       "      <td>3796.711</td>\n",
       "      <td>3845.087</td>\n",
       "      <td>3761.422</td>\n",
       "      <td>3843.597</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-29</th>\n",
       "      <td>3843.597</td>\n",
       "      <td>3842.823</td>\n",
       "      <td>3867.606</td>\n",
       "      <td>3839.206</td>\n",
       "      <td>3857.927</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8473 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose      Open   Highest    Lowest     Close\n",
       "Day                                                         \n",
       "1990-12-19              96.050    99.980    95.790    99.980\n",
       "1990-12-20     99.98   104.300   104.390    99.980   104.390\n",
       "1990-12-21    104.39   109.070   109.130   103.730   109.130\n",
       "1990-12-24    109.13   113.570   114.550   109.130   114.550\n",
       "1990-12-25    114.55   120.090   120.250   114.550   120.250\n",
       "...              ...       ...       ...       ...       ...\n",
       "2025-08-25  3825.759  3848.163  3883.562  3839.972  3883.562\n",
       "2025-08-26  3883.562  3871.471  3888.599  3859.758  3868.382\n",
       "2025-08-27  3868.382  3869.612  3887.198  3800.350  3800.350\n",
       "2025-08-28   3800.35  3796.711  3845.087  3761.422  3843.597\n",
       "2025-08-29  3843.597  3842.823  3867.606  3839.206  3857.927\n",
       "\n",
       "[8473 rows x 5 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "data = pd.read_csv('D:/笃行楼209/000001.csv')\n",
    "data['Day'] = pd.to_datetime(data['Day'],format='%Y/%m/%d')\n",
    "data.set_index('Day', inplace = True)\n",
    "data.sort_values(by = ['Day'],axis=0, ascending=True)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "aa2ded22",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2002-01-04</th>\n",
       "      <td>1645.970</td>\n",
       "      <td>1643.480</td>\n",
       "      <td>1643.500</td>\n",
       "      <td>1608.830</td>\n",
       "      <td>1611.390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-01-07</th>\n",
       "      <td>1611.390</td>\n",
       "      <td>1605.740</td>\n",
       "      <td>1605.740</td>\n",
       "      <td>1584.660</td>\n",
       "      <td>1596.760</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-01-08</th>\n",
       "      <td>1596.760</td>\n",
       "      <td>1594.870</td>\n",
       "      <td>1607.720</td>\n",
       "      <td>1582.450</td>\n",
       "      <td>1583.460</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-01-09</th>\n",
       "      <td>1583.460</td>\n",
       "      <td>1580.710</td>\n",
       "      <td>1580.720</td>\n",
       "      <td>1552.560</td>\n",
       "      <td>1561.350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-01-10</th>\n",
       "      <td>1561.350</td>\n",
       "      <td>1556.740</td>\n",
       "      <td>1580.010</td>\n",
       "      <td>1523.660</td>\n",
       "      <td>1576.440</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-21</th>\n",
       "      <td>3244.378</td>\n",
       "      <td>3256.833</td>\n",
       "      <td>3258.610</td>\n",
       "      <td>3229.069</td>\n",
       "      <td>3242.623</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-22</th>\n",
       "      <td>3242.623</td>\n",
       "      <td>3235.493</td>\n",
       "      <td>3235.501</td>\n",
       "      <td>3203.380</td>\n",
       "      <td>3213.624</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-23</th>\n",
       "      <td>3213.624</td>\n",
       "      <td>3237.599</td>\n",
       "      <td>3273.520</td>\n",
       "      <td>3229.572</td>\n",
       "      <td>3230.164</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-24</th>\n",
       "      <td>3230.164</td>\n",
       "      <td>3222.877</td>\n",
       "      <td>3260.011</td>\n",
       "      <td>3221.712</td>\n",
       "      <td>3252.626</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-27</th>\n",
       "      <td>3252.626</td>\n",
       "      <td>3256.614</td>\n",
       "      <td>3274.392</td>\n",
       "      <td>3250.601</td>\n",
       "      <td>3250.601</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5597 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose      Open   Highest    Lowest     Close\n",
       "Day                                                         \n",
       "2002-01-04  1645.970  1643.480  1643.500  1608.830  1611.390\n",
       "2002-01-07  1611.390  1605.740  1605.740  1584.660  1596.760\n",
       "2002-01-08  1596.760  1594.870  1607.720  1582.450  1583.460\n",
       "2002-01-09  1583.460  1580.710  1580.720  1552.560  1561.350\n",
       "2002-01-10  1561.350  1556.740  1580.010  1523.660  1576.440\n",
       "...              ...       ...       ...       ...       ...\n",
       "2025-01-21  3244.378  3256.833  3258.610  3229.069  3242.623\n",
       "2025-01-22  3242.623  3235.493  3235.501  3203.380  3213.624\n",
       "2025-01-23  3213.624  3237.599  3273.520  3229.572  3230.164\n",
       "2025-01-24  3230.164  3222.877  3260.011  3221.712  3252.626\n",
       "2025-01-27  3252.626  3256.614  3274.392  3250.601  3250.601\n",
       "\n",
       "[5597 rows x 5 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new = data['2002-01':'2025-01'].copy()\n",
    "data_new['Close'] = pd.to_numeric(data_new['Close'])\n",
    "data_new['Preclose'] = pd.to_numeric(data_new['Preclose'])\n",
    "data_new"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4da6cd6",
   "metadata": {},
   "source": [
    "# 多种日度收益率计算方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4944bff7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Pct_change_return</th>\n",
       "      <th>Apply_return</th>\n",
       "      <th>Diff_div_return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2002-01-04</th>\n",
       "      <td>1645.970</td>\n",
       "      <td>1643.480</td>\n",
       "      <td>1643.500</td>\n",
       "      <td>1608.830</td>\n",
       "      <td>1611.390</td>\n",
       "      <td>-0.021009</td>\n",
       "      <td>-0.021233</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.021009</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-01-07</th>\n",
       "      <td>1611.390</td>\n",
       "      <td>1605.740</td>\n",
       "      <td>1605.740</td>\n",
       "      <td>1584.660</td>\n",
       "      <td>1596.760</td>\n",
       "      <td>-0.009079</td>\n",
       "      <td>-0.009121</td>\n",
       "      <td>-0.009079</td>\n",
       "      <td>-0.009079</td>\n",
       "      <td>-0.009079</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-01-08</th>\n",
       "      <td>1596.760</td>\n",
       "      <td>1594.870</td>\n",
       "      <td>1607.720</td>\n",
       "      <td>1582.450</td>\n",
       "      <td>1583.460</td>\n",
       "      <td>-0.008329</td>\n",
       "      <td>-0.008364</td>\n",
       "      <td>-0.008329</td>\n",
       "      <td>-0.008329</td>\n",
       "      <td>-0.008329</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-01-09</th>\n",
       "      <td>1583.460</td>\n",
       "      <td>1580.710</td>\n",
       "      <td>1580.720</td>\n",
       "      <td>1552.560</td>\n",
       "      <td>1561.350</td>\n",
       "      <td>-0.013963</td>\n",
       "      <td>-0.014061</td>\n",
       "      <td>-0.013963</td>\n",
       "      <td>-0.013963</td>\n",
       "      <td>-0.013963</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-01-10</th>\n",
       "      <td>1561.350</td>\n",
       "      <td>1556.740</td>\n",
       "      <td>1580.010</td>\n",
       "      <td>1523.660</td>\n",
       "      <td>1576.440</td>\n",
       "      <td>0.009665</td>\n",
       "      <td>0.009618</td>\n",
       "      <td>0.009665</td>\n",
       "      <td>0.009665</td>\n",
       "      <td>0.009665</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-21</th>\n",
       "      <td>3244.378</td>\n",
       "      <td>3256.833</td>\n",
       "      <td>3258.610</td>\n",
       "      <td>3229.069</td>\n",
       "      <td>3242.623</td>\n",
       "      <td>-0.000541</td>\n",
       "      <td>-0.000541</td>\n",
       "      <td>-0.000541</td>\n",
       "      <td>-0.000541</td>\n",
       "      <td>-0.000541</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-22</th>\n",
       "      <td>3242.623</td>\n",
       "      <td>3235.493</td>\n",
       "      <td>3235.501</td>\n",
       "      <td>3203.380</td>\n",
       "      <td>3213.624</td>\n",
       "      <td>-0.008943</td>\n",
       "      <td>-0.008983</td>\n",
       "      <td>-0.008943</td>\n",
       "      <td>-0.008943</td>\n",
       "      <td>-0.008943</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-23</th>\n",
       "      <td>3213.624</td>\n",
       "      <td>3237.599</td>\n",
       "      <td>3273.520</td>\n",
       "      <td>3229.572</td>\n",
       "      <td>3230.164</td>\n",
       "      <td>0.005147</td>\n",
       "      <td>0.005134</td>\n",
       "      <td>0.005147</td>\n",
       "      <td>0.005147</td>\n",
       "      <td>0.005147</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-24</th>\n",
       "      <td>3230.164</td>\n",
       "      <td>3222.877</td>\n",
       "      <td>3260.011</td>\n",
       "      <td>3221.712</td>\n",
       "      <td>3252.626</td>\n",
       "      <td>0.006954</td>\n",
       "      <td>0.006930</td>\n",
       "      <td>0.006954</td>\n",
       "      <td>0.006954</td>\n",
       "      <td>0.006954</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-27</th>\n",
       "      <td>3252.626</td>\n",
       "      <td>3256.614</td>\n",
       "      <td>3274.392</td>\n",
       "      <td>3250.601</td>\n",
       "      <td>3250.601</td>\n",
       "      <td>-0.000623</td>\n",
       "      <td>-0.000623</td>\n",
       "      <td>-0.000623</td>\n",
       "      <td>-0.000623</td>\n",
       "      <td>-0.000623</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5597 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose      Open   Highest    Lowest     Close  Raw_return  \\\n",
       "Day                                                                        \n",
       "2002-01-04  1645.970  1643.480  1643.500  1608.830  1611.390   -0.021009   \n",
       "2002-01-07  1611.390  1605.740  1605.740  1584.660  1596.760   -0.009079   \n",
       "2002-01-08  1596.760  1594.870  1607.720  1582.450  1583.460   -0.008329   \n",
       "2002-01-09  1583.460  1580.710  1580.720  1552.560  1561.350   -0.013963   \n",
       "2002-01-10  1561.350  1556.740  1580.010  1523.660  1576.440    0.009665   \n",
       "...              ...       ...       ...       ...       ...         ...   \n",
       "2025-01-21  3244.378  3256.833  3258.610  3229.069  3242.623   -0.000541   \n",
       "2025-01-22  3242.623  3235.493  3235.501  3203.380  3213.624   -0.008943   \n",
       "2025-01-23  3213.624  3237.599  3273.520  3229.572  3230.164    0.005147   \n",
       "2025-01-24  3230.164  3222.877  3260.011  3221.712  3252.626    0.006954   \n",
       "2025-01-27  3252.626  3256.614  3274.392  3250.601  3250.601   -0.000623   \n",
       "\n",
       "            Log_return  Pct_change_return  Apply_return  Diff_div_return  \n",
       "Day                                                                       \n",
       "2002-01-04   -0.021233                NaN     -0.021009              NaN  \n",
       "2002-01-07   -0.009121          -0.009079     -0.009079        -0.009079  \n",
       "2002-01-08   -0.008364          -0.008329     -0.008329        -0.008329  \n",
       "2002-01-09   -0.014061          -0.013963     -0.013963        -0.013963  \n",
       "2002-01-10    0.009618           0.009665      0.009665         0.009665  \n",
       "...                ...                ...           ...              ...  \n",
       "2025-01-21   -0.000541          -0.000541     -0.000541        -0.000541  \n",
       "2025-01-22   -0.008983          -0.008943     -0.008943        -0.008943  \n",
       "2025-01-23    0.005134           0.005147      0.005147         0.005147  \n",
       "2025-01-24    0.006930           0.006954      0.006954         0.006954  \n",
       "2025-01-27   -0.000623          -0.000623     -0.000623        -0.000623  \n",
       "\n",
       "[5597 rows x 10 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算000001上证指数日收益率 - 方法1：直接使用向量化操作（最推荐的方式）\n",
    "data_new['Raw_return'] = data_new['Close'] / data_new['Preclose'] - 1\n",
    "data_new['Log_return'] = np.log(data_new['Close']) - np.log(data_new['Preclose'])\n",
    "\n",
    "# 方法2：使用pandas的pct_change函数计算收益率（适用于时间序列数据）\n",
    "# 注意：这种方法需要数据已经按时间排序\n",
    "data_new['Pct_change_return'] = data_new['Close'].pct_change()\n",
    "\n",
    "# 方法3：使用apply方法（不推荐，因为速度较慢）\n",
    "data_new['Apply_return'] = data_new.apply(lambda row: row['Close'] / row['Preclose'] - 1, axis=1)\n",
    "\n",
    "# 方法4：使用diff和div方法组合（另一种向量化操作）\n",
    "data_new['Diff_div_return'] = data_new['Close'].diff() / data_new['Close'].shift(1)\n",
    "\n",
    "# 比较不同方法计算结果的差异\n",
    "data_new"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f06e0934",
   "metadata": {},
   "source": [
    "# 循环与向量化方法比较"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6805069b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Pct_change_return</th>\n",
       "      <th>Apply_return</th>\n",
       "      <th>Diff_div_return</th>\n",
       "      <th>Loop_return</th>\n",
       "      <th>Loop_return2</th>\n",
       "      <th>Numpy_return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2002-01-04</th>\n",
       "      <td>1645.970</td>\n",
       "      <td>1643.480</td>\n",
       "      <td>1643.500</td>\n",
       "      <td>1608.830</td>\n",
       "      <td>1611.390</td>\n",
       "      <td>-0.021009</td>\n",
       "      <td>-0.021233</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.021009</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.021009</td>\n",
       "      <td>-0.021009</td>\n",
       "      <td>-0.021009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-01-07</th>\n",
       "      <td>1611.390</td>\n",
       "      <td>1605.740</td>\n",
       "      <td>1605.740</td>\n",
       "      <td>1584.660</td>\n",
       "      <td>1596.760</td>\n",
       "      <td>-0.009079</td>\n",
       "      <td>-0.009121</td>\n",
       "      <td>-0.009079</td>\n",
       "      <td>-0.009079</td>\n",
       "      <td>-0.009079</td>\n",
       "      <td>-0.009079</td>\n",
       "      <td>-0.009079</td>\n",
       "      <td>-0.009079</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-01-08</th>\n",
       "      <td>1596.760</td>\n",
       "      <td>1594.870</td>\n",
       "      <td>1607.720</td>\n",
       "      <td>1582.450</td>\n",
       "      <td>1583.460</td>\n",
       "      <td>-0.008329</td>\n",
       "      <td>-0.008364</td>\n",
       "      <td>-0.008329</td>\n",
       "      <td>-0.008329</td>\n",
       "      <td>-0.008329</td>\n",
       "      <td>-0.008329</td>\n",
       "      <td>-0.008329</td>\n",
       "      <td>-0.008329</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-01-09</th>\n",
       "      <td>1583.460</td>\n",
       "      <td>1580.710</td>\n",
       "      <td>1580.720</td>\n",
       "      <td>1552.560</td>\n",
       "      <td>1561.350</td>\n",
       "      <td>-0.013963</td>\n",
       "      <td>-0.014061</td>\n",
       "      <td>-0.013963</td>\n",
       "      <td>-0.013963</td>\n",
       "      <td>-0.013963</td>\n",
       "      <td>-0.013963</td>\n",
       "      <td>-0.013963</td>\n",
       "      <td>-0.013963</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-01-10</th>\n",
       "      <td>1561.350</td>\n",
       "      <td>1556.740</td>\n",
       "      <td>1580.010</td>\n",
       "      <td>1523.660</td>\n",
       "      <td>1576.440</td>\n",
       "      <td>0.009665</td>\n",
       "      <td>0.009618</td>\n",
       "      <td>0.009665</td>\n",
       "      <td>0.009665</td>\n",
       "      <td>0.009665</td>\n",
       "      <td>0.009665</td>\n",
       "      <td>0.009665</td>\n",
       "      <td>0.009665</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-21</th>\n",
       "      <td>3244.378</td>\n",
       "      <td>3256.833</td>\n",
       "      <td>3258.610</td>\n",
       "      <td>3229.069</td>\n",
       "      <td>3242.623</td>\n",
       "      <td>-0.000541</td>\n",
       "      <td>-0.000541</td>\n",
       "      <td>-0.000541</td>\n",
       "      <td>-0.000541</td>\n",
       "      <td>-0.000541</td>\n",
       "      <td>-0.000541</td>\n",
       "      <td>-0.000541</td>\n",
       "      <td>-0.000541</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-22</th>\n",
       "      <td>3242.623</td>\n",
       "      <td>3235.493</td>\n",
       "      <td>3235.501</td>\n",
       "      <td>3203.380</td>\n",
       "      <td>3213.624</td>\n",
       "      <td>-0.008943</td>\n",
       "      <td>-0.008983</td>\n",
       "      <td>-0.008943</td>\n",
       "      <td>-0.008943</td>\n",
       "      <td>-0.008943</td>\n",
       "      <td>-0.008943</td>\n",
       "      <td>-0.008943</td>\n",
       "      <td>-0.008943</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-23</th>\n",
       "      <td>3213.624</td>\n",
       "      <td>3237.599</td>\n",
       "      <td>3273.520</td>\n",
       "      <td>3229.572</td>\n",
       "      <td>3230.164</td>\n",
       "      <td>0.005147</td>\n",
       "      <td>0.005134</td>\n",
       "      <td>0.005147</td>\n",
       "      <td>0.005147</td>\n",
       "      <td>0.005147</td>\n",
       "      <td>0.005147</td>\n",
       "      <td>0.005147</td>\n",
       "      <td>0.005147</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-24</th>\n",
       "      <td>3230.164</td>\n",
       "      <td>3222.877</td>\n",
       "      <td>3260.011</td>\n",
       "      <td>3221.712</td>\n",
       "      <td>3252.626</td>\n",
       "      <td>0.006954</td>\n",
       "      <td>0.006930</td>\n",
       "      <td>0.006954</td>\n",
       "      <td>0.006954</td>\n",
       "      <td>0.006954</td>\n",
       "      <td>0.006954</td>\n",
       "      <td>0.006954</td>\n",
       "      <td>0.006954</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-27</th>\n",
       "      <td>3252.626</td>\n",
       "      <td>3256.614</td>\n",
       "      <td>3274.392</td>\n",
       "      <td>3250.601</td>\n",
       "      <td>3250.601</td>\n",
       "      <td>-0.000623</td>\n",
       "      <td>-0.000623</td>\n",
       "      <td>-0.000623</td>\n",
       "      <td>-0.000623</td>\n",
       "      <td>-0.000623</td>\n",
       "      <td>-0.000623</td>\n",
       "      <td>-0.000623</td>\n",
       "      <td>-0.000623</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5597 rows × 13 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose      Open   Highest    Lowest     Close  Raw_return  \\\n",
       "Day                                                                        \n",
       "2002-01-04  1645.970  1643.480  1643.500  1608.830  1611.390   -0.021009   \n",
       "2002-01-07  1611.390  1605.740  1605.740  1584.660  1596.760   -0.009079   \n",
       "2002-01-08  1596.760  1594.870  1607.720  1582.450  1583.460   -0.008329   \n",
       "2002-01-09  1583.460  1580.710  1580.720  1552.560  1561.350   -0.013963   \n",
       "2002-01-10  1561.350  1556.740  1580.010  1523.660  1576.440    0.009665   \n",
       "...              ...       ...       ...       ...       ...         ...   \n",
       "2025-01-21  3244.378  3256.833  3258.610  3229.069  3242.623   -0.000541   \n",
       "2025-01-22  3242.623  3235.493  3235.501  3203.380  3213.624   -0.008943   \n",
       "2025-01-23  3213.624  3237.599  3273.520  3229.572  3230.164    0.005147   \n",
       "2025-01-24  3230.164  3222.877  3260.011  3221.712  3252.626    0.006954   \n",
       "2025-01-27  3252.626  3256.614  3274.392  3250.601  3250.601   -0.000623   \n",
       "\n",
       "            Log_return  Pct_change_return  Apply_return  Diff_div_return  \\\n",
       "Day                                                                        \n",
       "2002-01-04   -0.021233                NaN     -0.021009              NaN   \n",
       "2002-01-07   -0.009121          -0.009079     -0.009079        -0.009079   \n",
       "2002-01-08   -0.008364          -0.008329     -0.008329        -0.008329   \n",
       "2002-01-09   -0.014061          -0.013963     -0.013963        -0.013963   \n",
       "2002-01-10    0.009618           0.009665      0.009665         0.009665   \n",
       "...                ...                ...           ...              ...   \n",
       "2025-01-21   -0.000541          -0.000541     -0.000541        -0.000541   \n",
       "2025-01-22   -0.008983          -0.008943     -0.008943        -0.008943   \n",
       "2025-01-23    0.005134           0.005147      0.005147         0.005147   \n",
       "2025-01-24    0.006930           0.006954      0.006954         0.006954   \n",
       "2025-01-27   -0.000623          -0.000623     -0.000623        -0.000623   \n",
       "\n",
       "            Loop_return  Loop_return2  Numpy_return  \n",
       "Day                                                  \n",
       "2002-01-04    -0.021009     -0.021009     -0.021009  \n",
       "2002-01-07    -0.009079     -0.009079     -0.009079  \n",
       "2002-01-08    -0.008329     -0.008329     -0.008329  \n",
       "2002-01-09    -0.013963     -0.013963     -0.013963  \n",
       "2002-01-10     0.009665      0.009665      0.009665  \n",
       "...                 ...           ...           ...  \n",
       "2025-01-21    -0.000541     -0.000541     -0.000541  \n",
       "2025-01-22    -0.008943     -0.008943     -0.008943  \n",
       "2025-01-23     0.005147      0.005147      0.005147  \n",
       "2025-01-24     0.006954      0.006954      0.006954  \n",
       "2025-01-27    -0.000623     -0.000623     -0.000623  \n",
       "\n",
       "[5597 rows x 13 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法5：使用for循环计算收益率（不推荐，效率低）\n",
    "# 这种方法在大数据集上会非常慢，仅作为教学示例\n",
    "\n",
    "# 创建新列存储结果\n",
    "if 'Loop_return' not in data_new.columns:\n",
    "    data_new['Loop_return'] = np.nan\n",
    "\n",
    "# 使用for循环计算\n",
    "for i in range(len(data_new)):\n",
    "    data_new.iloc[i, data_new.columns.get_loc('Loop_return')] = data_new.iloc[i, data_new.columns.get_loc('Close')] / data_new.iloc[i, data_new.columns.get_loc('Preclose')] - 1\n",
    "\n",
    "# 方法6：使用zip和enumerate组合（比纯for循环更Pythonic）\n",
    "close_values = data_new['Close'].values\n",
    "preclose_values = data_new['Preclose'].values\n",
    "loop_return_values = []\n",
    "\n",
    "for i, (close, preclose) in enumerate(zip(close_values, preclose_values)):\n",
    "    if preclose != 0 and not np.isnan(preclose):\n",
    "        loop_return_values.append(close / preclose - 1)\n",
    "    else:\n",
    "        loop_return_values.append(np.nan)\n",
    "\n",
    "data_new['Loop_return2'] = loop_return_values\n",
    "\n",
    "# 方法7：使用numpy的向量化操作（高效且简洁）\n",
    "data_new['Numpy_return'] = (data_new['Close'].values / data_new['Preclose'].values) - 1\n",
    "\n",
    "# 显示结果\n",
    "data_new"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13043e20",
   "metadata": {},
   "source": [
    "# 月度收益率计算resample方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "eab6272e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Raw_Return</th>\n",
       "      <th>Year</th>\n",
       "      <th>Month</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2002-01-31</th>\n",
       "      <td>-0.098440</td>\n",
       "      <td>-0.093750</td>\n",
       "      <td>2002</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-02-28</th>\n",
       "      <td>0.021908</td>\n",
       "      <td>0.022150</td>\n",
       "      <td>2002</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-03-31</th>\n",
       "      <td>0.050640</td>\n",
       "      <td>0.051945</td>\n",
       "      <td>2002</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-04-30</th>\n",
       "      <td>0.039037</td>\n",
       "      <td>0.039809</td>\n",
       "      <td>2002</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-05-31</th>\n",
       "      <td>-0.095578</td>\n",
       "      <td>-0.091153</td>\n",
       "      <td>2002</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            Log_return  Raw_Return  Year  Month\n",
       "Day                                            \n",
       "2002-01-31   -0.098440   -0.093750  2002      1\n",
       "2002-02-28    0.021908    0.022150  2002      2\n",
       "2002-03-31    0.050640    0.051945  2002      3\n",
       "2002-04-30    0.039037    0.039809  2002      4\n",
       "2002-05-31   -0.095578   -0.091153  2002      5"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法1：使用resample函数计算月度对数收益率并转换为原始收益率\n",
    "# 这种方法适合对数收益率，因为对数收益率可以直接相加\n",
    "Month_data1 = data_new.resample('M')['Log_return'].sum().to_frame(name='Log_return') \n",
    "Month_data1['Raw_Return'] = np.exp(Month_data1['Log_return']) - 1\n",
    "\n",
    "# 添加年月信息便于分析\n",
    "Month_data1['Year'] = Month_data1.index.year\n",
    "Month_data1['Month'] = Month_data1.index.month\n",
    "\n",
    "# 显示结果\n",
    "Month_data1.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0ed4baa3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Year</th>\n",
       "      <th>Month</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2002-01-31</th>\n",
       "      <td>1491.66</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2002</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-02-28</th>\n",
       "      <td>1524.70</td>\n",
       "      <td>1491.66</td>\n",
       "      <td>0.022150</td>\n",
       "      <td>0.021908</td>\n",
       "      <td>2002</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-03-31</th>\n",
       "      <td>1603.90</td>\n",
       "      <td>1524.70</td>\n",
       "      <td>0.051945</td>\n",
       "      <td>0.050640</td>\n",
       "      <td>2002</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-04-30</th>\n",
       "      <td>1667.75</td>\n",
       "      <td>1603.90</td>\n",
       "      <td>0.039809</td>\n",
       "      <td>0.039037</td>\n",
       "      <td>2002</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-05-31</th>\n",
       "      <td>1515.73</td>\n",
       "      <td>1667.75</td>\n",
       "      <td>-0.091153</td>\n",
       "      <td>-0.095578</td>\n",
       "      <td>2002</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              Close  Preclose  Raw_return  Log_return  Year  Month\n",
       "Day                                                               \n",
       "2002-01-31  1491.66       NaN         NaN         NaN  2002      1\n",
       "2002-02-28  1524.70   1491.66    0.022150    0.021908  2002      2\n",
       "2002-03-31  1603.90   1524.70    0.051945    0.050640  2002      3\n",
       "2002-04-30  1667.75   1603.90    0.039809    0.039037  2002      4\n",
       "2002-05-31  1515.73   1667.75   -0.091153   -0.095578  2002      5"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法2：使用resample取月末价格计算月度收益率\n",
    "# 这种方法直接使用月末价格计算收益率，更符合金融实践\n",
    "Month_data2 = data_new.resample('M')['Close'].last().to_frame()\n",
    "Month_data2['Preclose'] = Month_data2['Close'].shift(1)\n",
    "Month_data2['Raw_return'] = Month_data2['Close'] / Month_data2['Preclose'] - 1\n",
    "Month_data2['Log_return'] = np.log(Month_data2['Close']) - np.log(Month_data2['Preclose'])\n",
    "\n",
    "# 添加年月信息\n",
    "Month_data2['Year'] = Month_data2.index.year\n",
    "Month_data2['Month'] = Month_data2.index.month\n",
    "\n",
    "# 显示结果\n",
    "Month_data2.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a36bdd5",
   "metadata": {},
   "source": [
    "# groupy by 方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d7200c13",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Pct_change_return</th>\n",
       "      <th>Apply_return</th>\n",
       "      <th>Diff_div_return</th>\n",
       "      <th>Loop_return</th>\n",
       "      <th>Loop_return2</th>\n",
       "      <th>Numpy_return</th>\n",
       "      <th>year</th>\n",
       "      <th>month</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2002-01-04</th>\n",
       "      <td>1645.970</td>\n",
       "      <td>1643.480</td>\n",
       "      <td>1643.500</td>\n",
       "      <td>1608.830</td>\n",
       "      <td>1611.390</td>\n",
       "      <td>-0.021009</td>\n",
       "      <td>-0.021233</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.021009</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.021009</td>\n",
       "      <td>-0.021009</td>\n",
       "      <td>-0.021009</td>\n",
       "      <td>2002</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-01-07</th>\n",
       "      <td>1611.390</td>\n",
       "      <td>1605.740</td>\n",
       "      <td>1605.740</td>\n",
       "      <td>1584.660</td>\n",
       "      <td>1596.760</td>\n",
       "      <td>-0.009079</td>\n",
       "      <td>-0.009121</td>\n",
       "      <td>-0.009079</td>\n",
       "      <td>-0.009079</td>\n",
       "      <td>-0.009079</td>\n",
       "      <td>-0.009079</td>\n",
       "      <td>-0.009079</td>\n",
       "      <td>-0.009079</td>\n",
       "      <td>2002</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-01-08</th>\n",
       "      <td>1596.760</td>\n",
       "      <td>1594.870</td>\n",
       "      <td>1607.720</td>\n",
       "      <td>1582.450</td>\n",
       "      <td>1583.460</td>\n",
       "      <td>-0.008329</td>\n",
       "      <td>-0.008364</td>\n",
       "      <td>-0.008329</td>\n",
       "      <td>-0.008329</td>\n",
       "      <td>-0.008329</td>\n",
       "      <td>-0.008329</td>\n",
       "      <td>-0.008329</td>\n",
       "      <td>-0.008329</td>\n",
       "      <td>2002</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-01-09</th>\n",
       "      <td>1583.460</td>\n",
       "      <td>1580.710</td>\n",
       "      <td>1580.720</td>\n",
       "      <td>1552.560</td>\n",
       "      <td>1561.350</td>\n",
       "      <td>-0.013963</td>\n",
       "      <td>-0.014061</td>\n",
       "      <td>-0.013963</td>\n",
       "      <td>-0.013963</td>\n",
       "      <td>-0.013963</td>\n",
       "      <td>-0.013963</td>\n",
       "      <td>-0.013963</td>\n",
       "      <td>-0.013963</td>\n",
       "      <td>2002</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-01-10</th>\n",
       "      <td>1561.350</td>\n",
       "      <td>1556.740</td>\n",
       "      <td>1580.010</td>\n",
       "      <td>1523.660</td>\n",
       "      <td>1576.440</td>\n",
       "      <td>0.009665</td>\n",
       "      <td>0.009618</td>\n",
       "      <td>0.009665</td>\n",
       "      <td>0.009665</td>\n",
       "      <td>0.009665</td>\n",
       "      <td>0.009665</td>\n",
       "      <td>0.009665</td>\n",
       "      <td>0.009665</td>\n",
       "      <td>2002</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-21</th>\n",
       "      <td>3244.378</td>\n",
       "      <td>3256.833</td>\n",
       "      <td>3258.610</td>\n",
       "      <td>3229.069</td>\n",
       "      <td>3242.623</td>\n",
       "      <td>-0.000541</td>\n",
       "      <td>-0.000541</td>\n",
       "      <td>-0.000541</td>\n",
       "      <td>-0.000541</td>\n",
       "      <td>-0.000541</td>\n",
       "      <td>-0.000541</td>\n",
       "      <td>-0.000541</td>\n",
       "      <td>-0.000541</td>\n",
       "      <td>2025</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-22</th>\n",
       "      <td>3242.623</td>\n",
       "      <td>3235.493</td>\n",
       "      <td>3235.501</td>\n",
       "      <td>3203.380</td>\n",
       "      <td>3213.624</td>\n",
       "      <td>-0.008943</td>\n",
       "      <td>-0.008983</td>\n",
       "      <td>-0.008943</td>\n",
       "      <td>-0.008943</td>\n",
       "      <td>-0.008943</td>\n",
       "      <td>-0.008943</td>\n",
       "      <td>-0.008943</td>\n",
       "      <td>-0.008943</td>\n",
       "      <td>2025</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-23</th>\n",
       "      <td>3213.624</td>\n",
       "      <td>3237.599</td>\n",
       "      <td>3273.520</td>\n",
       "      <td>3229.572</td>\n",
       "      <td>3230.164</td>\n",
       "      <td>0.005147</td>\n",
       "      <td>0.005134</td>\n",
       "      <td>0.005147</td>\n",
       "      <td>0.005147</td>\n",
       "      <td>0.005147</td>\n",
       "      <td>0.005147</td>\n",
       "      <td>0.005147</td>\n",
       "      <td>0.005147</td>\n",
       "      <td>2025</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-24</th>\n",
       "      <td>3230.164</td>\n",
       "      <td>3222.877</td>\n",
       "      <td>3260.011</td>\n",
       "      <td>3221.712</td>\n",
       "      <td>3252.626</td>\n",
       "      <td>0.006954</td>\n",
       "      <td>0.006930</td>\n",
       "      <td>0.006954</td>\n",
       "      <td>0.006954</td>\n",
       "      <td>0.006954</td>\n",
       "      <td>0.006954</td>\n",
       "      <td>0.006954</td>\n",
       "      <td>0.006954</td>\n",
       "      <td>2025</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-27</th>\n",
       "      <td>3252.626</td>\n",
       "      <td>3256.614</td>\n",
       "      <td>3274.392</td>\n",
       "      <td>3250.601</td>\n",
       "      <td>3250.601</td>\n",
       "      <td>-0.000623</td>\n",
       "      <td>-0.000623</td>\n",
       "      <td>-0.000623</td>\n",
       "      <td>-0.000623</td>\n",
       "      <td>-0.000623</td>\n",
       "      <td>-0.000623</td>\n",
       "      <td>-0.000623</td>\n",
       "      <td>-0.000623</td>\n",
       "      <td>2025</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5597 rows × 15 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose      Open   Highest    Lowest     Close  Raw_return  \\\n",
       "Day                                                                        \n",
       "2002-01-04  1645.970  1643.480  1643.500  1608.830  1611.390   -0.021009   \n",
       "2002-01-07  1611.390  1605.740  1605.740  1584.660  1596.760   -0.009079   \n",
       "2002-01-08  1596.760  1594.870  1607.720  1582.450  1583.460   -0.008329   \n",
       "2002-01-09  1583.460  1580.710  1580.720  1552.560  1561.350   -0.013963   \n",
       "2002-01-10  1561.350  1556.740  1580.010  1523.660  1576.440    0.009665   \n",
       "...              ...       ...       ...       ...       ...         ...   \n",
       "2025-01-21  3244.378  3256.833  3258.610  3229.069  3242.623   -0.000541   \n",
       "2025-01-22  3242.623  3235.493  3235.501  3203.380  3213.624   -0.008943   \n",
       "2025-01-23  3213.624  3237.599  3273.520  3229.572  3230.164    0.005147   \n",
       "2025-01-24  3230.164  3222.877  3260.011  3221.712  3252.626    0.006954   \n",
       "2025-01-27  3252.626  3256.614  3274.392  3250.601  3250.601   -0.000623   \n",
       "\n",
       "            Log_return  Pct_change_return  Apply_return  Diff_div_return  \\\n",
       "Day                                                                        \n",
       "2002-01-04   -0.021233                NaN     -0.021009              NaN   \n",
       "2002-01-07   -0.009121          -0.009079     -0.009079        -0.009079   \n",
       "2002-01-08   -0.008364          -0.008329     -0.008329        -0.008329   \n",
       "2002-01-09   -0.014061          -0.013963     -0.013963        -0.013963   \n",
       "2002-01-10    0.009618           0.009665      0.009665         0.009665   \n",
       "...                ...                ...           ...              ...   \n",
       "2025-01-21   -0.000541          -0.000541     -0.000541        -0.000541   \n",
       "2025-01-22   -0.008983          -0.008943     -0.008943        -0.008943   \n",
       "2025-01-23    0.005134           0.005147      0.005147         0.005147   \n",
       "2025-01-24    0.006930           0.006954      0.006954         0.006954   \n",
       "2025-01-27   -0.000623          -0.000623     -0.000623        -0.000623   \n",
       "\n",
       "            Loop_return  Loop_return2  Numpy_return  year  month  \n",
       "Day                                                               \n",
       "2002-01-04    -0.021009     -0.021009     -0.021009  2002      1  \n",
       "2002-01-07    -0.009079     -0.009079     -0.009079  2002      1  \n",
       "2002-01-08    -0.008329     -0.008329     -0.008329  2002      1  \n",
       "2002-01-09    -0.013963     -0.013963     -0.013963  2002      1  \n",
       "2002-01-10     0.009665      0.009665      0.009665  2002      1  \n",
       "...                 ...           ...           ...   ...    ...  \n",
       "2025-01-21    -0.000541     -0.000541     -0.000541  2025      1  \n",
       "2025-01-22    -0.008943     -0.008943     -0.008943  2025      1  \n",
       "2025-01-23     0.005147      0.005147      0.005147  2025      1  \n",
       "2025-01-24     0.006954      0.006954      0.006954  2025      1  \n",
       "2025-01-27    -0.000623     -0.000623     -0.000623  2025      1  \n",
       "\n",
       "[5597 rows x 15 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# “1990-12-12”日期格式 里面的year年份 month月份 day 直接提出取来\n",
    "data_new2 = data_new.copy()\n",
    "data_new2['year'] = data_new2.index.year\n",
    "data_new2['month'] = data_new2.index.month\n",
    "data_new2\n",
    "# 使用的时间、日期格式提取 字符串提出的方式 前四个字符当作年份 6-7字符是月份 提取出来的是字符串 变成数值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b8a64586",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Raw_Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>year</th>\n",
       "      <th>month</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">2002</th>\n",
       "      <th>1</th>\n",
       "      <td>-0.098440</td>\n",
       "      <td>-0.093750</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.021908</td>\n",
       "      <td>0.022150</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.050640</td>\n",
       "      <td>0.051945</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.039037</td>\n",
       "      <td>0.039809</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>-0.095578</td>\n",
       "      <td>-0.091153</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">2024</th>\n",
       "      <th>9</th>\n",
       "      <td>0.160338</td>\n",
       "      <td>0.173908</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>-0.017132</td>\n",
       "      <td>-0.016986</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>0.014118</td>\n",
       "      <td>0.014218</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>0.007579</td>\n",
       "      <td>0.007608</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025</th>\n",
       "      <th>1</th>\n",
       "      <td>-0.030647</td>\n",
       "      <td>-0.030182</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>277 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Log_return  Raw_Return\n",
       "year month                        \n",
       "2002 1       -0.098440   -0.093750\n",
       "     2        0.021908    0.022150\n",
       "     3        0.050640    0.051945\n",
       "     4        0.039037    0.039809\n",
       "     5       -0.095578   -0.091153\n",
       "...                ...         ...\n",
       "2024 9        0.160338    0.173908\n",
       "     10      -0.017132   -0.016986\n",
       "     11       0.014118    0.014218\n",
       "     12       0.007579    0.007608\n",
       "2025 1       -0.030647   -0.030182\n",
       "\n",
       "[277 rows x 2 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法3：使用groupby函数按年月分组计算月度收益率\n",
    "# 首先提取年月信息\n",
    "data_new3 = data_new.copy()\n",
    "data_new3['year'] = data_new3.index.year\n",
    "data_new3['month'] = data_new3.index.month\n",
    "\n",
    "# 使用groupby按年月分组，然后对每组的对数收益率求和\n",
    "Month_data3 = data_new3.groupby(['year', 'month'])['Log_return'].sum().to_frame()\n",
    "Month_data3['Raw_Return'] = np.exp(Month_data3['Log_return']) - 1\n",
    "\n",
    "# 显示结果\n",
    "Month_data3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e4472ab0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "方法4结果:\n",
      "            Log_return  Raw_Return\n",
      "year month                        \n",
      "2002 1       -0.098440   -0.093750\n",
      "     2        0.021908    0.022150\n",
      "     3        0.050640    0.051945\n",
      "     4        0.039037    0.039809\n",
      "     5       -0.095578   -0.091153\n",
      "\n",
      "方法5结果 (包含多个统计量):\n",
      "           Log_return                           Raw_return          \n",
      "                  sum      mean       std count       mean       std\n",
      "year month                                                          \n",
      "2002 1      -0.098440 -0.004922  0.031413    20  -0.004440  0.031574\n",
      "     2       0.021908  0.002191  0.012963    10   0.002269  0.012973\n",
      "     3       0.050640  0.002411  0.016865    21   0.002550  0.016878\n",
      "     4       0.039037  0.001774  0.010928    22   0.001833  0.010976\n",
      "     5      -0.095578 -0.005310  0.013483    18  -0.005210  0.013469\n"
     ]
    }
   ],
   "source": [
    "# 方法4：使用apply和lambda函数进行更灵活的分组计算\n",
    "# 这种方法可以对每个月的数据进行更复杂的操作\n",
    "Month_data4 = pd.DataFrame(\n",
    "    data_new3.groupby(['year', 'month'])['Log_return'].apply(lambda x: sum(x)))\n",
    "Month_data4.columns = ['Log_return']\n",
    "Month_data4['Raw_Return'] = np.exp(Month_data4['Log_return']) - 1\n",
    "\n",
    "# 方法5：使用agg函数同时计算多个统计量\n",
    "Month_data5 = data_new3.groupby(['year', 'month']).agg({\n",
    "    'Log_return': ['sum', 'mean', 'std', 'count'],\n",
    "    'Raw_return': ['mean', 'std']\n",
    "})\n",
    "\n",
    "# 显示结果\n",
    "print(\"方法4结果:\")\n",
    "print(Month_data4.head())\n",
    "print(\"\\n方法5结果 (包含多个统计量):\")\n",
    "print(Month_data5.head())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "576d9c3e",
   "metadata": {},
   "source": [
    "# 季度收益"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "24187b3e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "季度对数收益率汇总:\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Raw_Return</th>\n",
       "      <th>Year</th>\n",
       "      <th>Quarter</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2002-03-31</th>\n",
       "      <td>-0.025892</td>\n",
       "      <td>-0.025559</td>\n",
       "      <td>2002</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-06-30</th>\n",
       "      <td>0.077277</td>\n",
       "      <td>0.080342</td>\n",
       "      <td>2002</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-09-30</th>\n",
       "      <td>-0.091266</td>\n",
       "      <td>-0.087225</td>\n",
       "      <td>2002</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-12-31</th>\n",
       "      <td>-0.152694</td>\n",
       "      <td>-0.141608</td>\n",
       "      <td>2002</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2003-03-31</th>\n",
       "      <td>0.106738</td>\n",
       "      <td>0.112643</td>\n",
       "      <td>2003</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-03-31</th>\n",
       "      <td>0.022019</td>\n",
       "      <td>0.022263</td>\n",
       "      <td>2024</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-06-30</th>\n",
       "      <td>-0.024554</td>\n",
       "      <td>-0.024255</td>\n",
       "      <td>2024</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-09-30</th>\n",
       "      <td>0.117234</td>\n",
       "      <td>0.124383</td>\n",
       "      <td>2024</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-31</th>\n",
       "      <td>0.004565</td>\n",
       "      <td>0.004575</td>\n",
       "      <td>2024</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-03-31</th>\n",
       "      <td>-0.030647</td>\n",
       "      <td>-0.030182</td>\n",
       "      <td>2025</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>93 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Log_return  Raw_Return  Year  Quarter\n",
       "Day                                              \n",
       "2002-03-31   -0.025892   -0.025559  2002        1\n",
       "2002-06-30    0.077277    0.080342  2002        2\n",
       "2002-09-30   -0.091266   -0.087225  2002        3\n",
       "2002-12-31   -0.152694   -0.141608  2002        4\n",
       "2003-03-31    0.106738    0.112643  2003        1\n",
       "...                ...         ...   ...      ...\n",
       "2024-03-31    0.022019    0.022263  2024        1\n",
       "2024-06-30   -0.024554   -0.024255  2024        2\n",
       "2024-09-30    0.117234    0.124383  2024        3\n",
       "2024-12-31    0.004565    0.004575  2024        4\n",
       "2025-03-31   -0.030647   -0.030182  2025        1\n",
       "\n",
       "[93 rows x 4 columns]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "季度末价格计算的收益率:\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2002-03-31</th>\n",
       "      <td>1603.9000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-06-30</th>\n",
       "      <td>1732.7600</td>\n",
       "      <td>1603.9000</td>\n",
       "      <td>0.080342</td>\n",
       "      <td>0.077277</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-09-30</th>\n",
       "      <td>1581.6200</td>\n",
       "      <td>1732.7600</td>\n",
       "      <td>-0.087225</td>\n",
       "      <td>-0.091266</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-12-31</th>\n",
       "      <td>1357.6500</td>\n",
       "      <td>1581.6200</td>\n",
       "      <td>-0.141608</td>\n",
       "      <td>-0.152694</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2003-03-31</th>\n",
       "      <td>1510.5800</td>\n",
       "      <td>1357.6500</td>\n",
       "      <td>0.112643</td>\n",
       "      <td>0.106738</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-03-31</th>\n",
       "      <td>3041.1669</td>\n",
       "      <td>2974.9348</td>\n",
       "      <td>0.022263</td>\n",
       "      <td>0.022019</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-06-30</th>\n",
       "      <td>2967.4028</td>\n",
       "      <td>3041.1669</td>\n",
       "      <td>-0.024255</td>\n",
       "      <td>-0.024554</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-09-30</th>\n",
       "      <td>3336.4970</td>\n",
       "      <td>2967.4028</td>\n",
       "      <td>0.124383</td>\n",
       "      <td>0.117234</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-31</th>\n",
       "      <td>3351.7630</td>\n",
       "      <td>3336.4970</td>\n",
       "      <td>0.004575</td>\n",
       "      <td>0.004565</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-03-31</th>\n",
       "      <td>3250.6010</td>\n",
       "      <td>3351.7630</td>\n",
       "      <td>-0.030182</td>\n",
       "      <td>-0.030647</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>93 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                Close   Preclose  Raw_return  Log_return\n",
       "Day                                                     \n",
       "2002-03-31  1603.9000        NaN         NaN         NaN\n",
       "2002-06-30  1732.7600  1603.9000    0.080342    0.077277\n",
       "2002-09-30  1581.6200  1732.7600   -0.087225   -0.091266\n",
       "2002-12-31  1357.6500  1581.6200   -0.141608   -0.152694\n",
       "2003-03-31  1510.5800  1357.6500    0.112643    0.106738\n",
       "...               ...        ...         ...         ...\n",
       "2024-03-31  3041.1669  2974.9348    0.022263    0.022019\n",
       "2024-06-30  2967.4028  3041.1669   -0.024255   -0.024554\n",
       "2024-09-30  3336.4970  2967.4028    0.124383    0.117234\n",
       "2024-12-31  3351.7630  3336.4970    0.004575    0.004565\n",
       "2025-03-31  3250.6010  3351.7630   -0.030182   -0.030647\n",
       "\n",
       "[93 rows x 4 columns]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算季度收益率\n",
    "# 方法1：使用resample函数的'QE'参数（季度末）\n",
    "Quarter_data1 = data_new.resample('Q')['Log_return'].sum().to_frame(name='Log_return')\n",
    "Quarter_data1['Raw_Return'] = np.exp(Quarter_data1['Log_return']) - 1\n",
    "Quarter_data1['Year'] = Quarter_data1.index.year\n",
    "Quarter_data1['Quarter'] = Quarter_data1.index.quarter\n",
    "\n",
    "# 方法2：使用季度末价格计算\n",
    "Quarter_data2 = data_new.resample('Q')['Close'].last().to_frame()\n",
    "Quarter_data2['Preclose'] = Quarter_data2['Close'].shift(1)\n",
    "Quarter_data2['Raw_return'] = Quarter_data2['Close'] / Quarter_data2['Preclose'] - 1\n",
    "Quarter_data2['Log_return'] = np.log(Quarter_data2['Close']) - np.log(Quarter_data2['Preclose'])\n",
    "\n",
    "# 显示结果\n",
    "print(\"季度对数收益率汇总:\")\n",
    "Quarter_data1\n",
    "print(\"\\n季度末价格计算的收益率:\")\n",
    "Quarter_data2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eddbf597",
   "metadata": {},
   "source": [
    "# 年度收益"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "01242251",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "年度对数收益率汇总:\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Raw_Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2002-12-31</th>\n",
       "      <td>-0.192575</td>\n",
       "      <td>-0.175167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2003-12-31</th>\n",
       "      <td>0.097735</td>\n",
       "      <td>0.102670</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-12-31</th>\n",
       "      <td>-0.167233</td>\n",
       "      <td>-0.153997</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2005-12-31</th>\n",
       "      <td>-0.086924</td>\n",
       "      <td>-0.083253</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006-12-31</th>\n",
       "      <td>0.834792</td>\n",
       "      <td>1.304334</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2007-12-31</th>\n",
       "      <td>0.676302</td>\n",
       "      <td>0.966593</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008-12-31</th>\n",
       "      <td>-1.061146</td>\n",
       "      <td>-0.653941</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009-12-31</th>\n",
       "      <td>0.587690</td>\n",
       "      <td>0.799825</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2010-12-31</th>\n",
       "      <td>-0.154470</td>\n",
       "      <td>-0.143131</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-12-31</th>\n",
       "      <td>-0.244307</td>\n",
       "      <td>-0.216753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-12-31</th>\n",
       "      <td>0.031203</td>\n",
       "      <td>0.031695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-12-31</th>\n",
       "      <td>-0.069878</td>\n",
       "      <td>-0.067493</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014-12-31</th>\n",
       "      <td>0.424412</td>\n",
       "      <td>0.528691</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-31</th>\n",
       "      <td>0.089965</td>\n",
       "      <td>0.094136</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-12-31</th>\n",
       "      <td>-0.131319</td>\n",
       "      <td>-0.123062</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-31</th>\n",
       "      <td>0.063517</td>\n",
       "      <td>0.065578</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-12-31</th>\n",
       "      <td>-0.282245</td>\n",
       "      <td>-0.245911</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-12-31</th>\n",
       "      <td>0.201333</td>\n",
       "      <td>0.223032</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-12-31</th>\n",
       "      <td>0.129858</td>\n",
       "      <td>0.138667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-12-31</th>\n",
       "      <td>0.046884</td>\n",
       "      <td>0.048001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-31</th>\n",
       "      <td>-0.163991</td>\n",
       "      <td>-0.151250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-12-31</th>\n",
       "      <td>-0.037709</td>\n",
       "      <td>-0.037007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-31</th>\n",
       "      <td>0.119264</td>\n",
       "      <td>0.126668</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-12-31</th>\n",
       "      <td>-0.030647</td>\n",
       "      <td>-0.030182</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            Log_return  Raw_Return\n",
       "Day                               \n",
       "2002-12-31   -0.192575   -0.175167\n",
       "2003-12-31    0.097735    0.102670\n",
       "2004-12-31   -0.167233   -0.153997\n",
       "2005-12-31   -0.086924   -0.083253\n",
       "2006-12-31    0.834792    1.304334\n",
       "2007-12-31    0.676302    0.966593\n",
       "2008-12-31   -1.061146   -0.653941\n",
       "2009-12-31    0.587690    0.799825\n",
       "2010-12-31   -0.154470   -0.143131\n",
       "2011-12-31   -0.244307   -0.216753\n",
       "2012-12-31    0.031203    0.031695\n",
       "2013-12-31   -0.069878   -0.067493\n",
       "2014-12-31    0.424412    0.528691\n",
       "2015-12-31    0.089965    0.094136\n",
       "2016-12-31   -0.131319   -0.123062\n",
       "2017-12-31    0.063517    0.065578\n",
       "2018-12-31   -0.282245   -0.245911\n",
       "2019-12-31    0.201333    0.223032\n",
       "2020-12-31    0.129858    0.138667\n",
       "2021-12-31    0.046884    0.048001\n",
       "2022-12-31   -0.163991   -0.151250\n",
       "2023-12-31   -0.037709   -0.037007\n",
       "2024-12-31    0.119264    0.126668\n",
       "2025-12-31   -0.030647   -0.030182"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "年末价格计算的收益率:\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2002-12-31</th>\n",
       "      <td>1357.6500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2003-12-31</th>\n",
       "      <td>1497.0400</td>\n",
       "      <td>1357.6500</td>\n",
       "      <td>0.102670</td>\n",
       "      <td>0.097735</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-12-31</th>\n",
       "      <td>1266.5000</td>\n",
       "      <td>1497.0400</td>\n",
       "      <td>-0.153997</td>\n",
       "      <td>-0.167233</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2005-12-31</th>\n",
       "      <td>1161.0600</td>\n",
       "      <td>1266.5000</td>\n",
       "      <td>-0.083253</td>\n",
       "      <td>-0.086924</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006-12-31</th>\n",
       "      <td>2675.4700</td>\n",
       "      <td>1161.0600</td>\n",
       "      <td>1.304334</td>\n",
       "      <td>0.834792</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2007-12-31</th>\n",
       "      <td>5261.5600</td>\n",
       "      <td>2675.4700</td>\n",
       "      <td>0.966593</td>\n",
       "      <td>0.676302</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008-12-31</th>\n",
       "      <td>1820.8100</td>\n",
       "      <td>5261.5600</td>\n",
       "      <td>-0.653941</td>\n",
       "      <td>-1.061146</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009-12-31</th>\n",
       "      <td>3277.1400</td>\n",
       "      <td>1820.8100</td>\n",
       "      <td>0.799825</td>\n",
       "      <td>0.587690</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2010-12-31</th>\n",
       "      <td>2808.0800</td>\n",
       "      <td>3277.1400</td>\n",
       "      <td>-0.143131</td>\n",
       "      <td>-0.154470</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-12-31</th>\n",
       "      <td>2199.4200</td>\n",
       "      <td>2808.0800</td>\n",
       "      <td>-0.216753</td>\n",
       "      <td>-0.244307</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-12-31</th>\n",
       "      <td>2269.1300</td>\n",
       "      <td>2199.4200</td>\n",
       "      <td>0.031695</td>\n",
       "      <td>0.031203</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-12-31</th>\n",
       "      <td>2115.9800</td>\n",
       "      <td>2269.1300</td>\n",
       "      <td>-0.067493</td>\n",
       "      <td>-0.069878</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014-12-31</th>\n",
       "      <td>3234.6800</td>\n",
       "      <td>2115.9800</td>\n",
       "      <td>0.528691</td>\n",
       "      <td>0.424412</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-31</th>\n",
       "      <td>3539.1800</td>\n",
       "      <td>3234.6800</td>\n",
       "      <td>0.094136</td>\n",
       "      <td>0.089965</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-12-31</th>\n",
       "      <td>3103.6400</td>\n",
       "      <td>3539.1800</td>\n",
       "      <td>-0.123062</td>\n",
       "      <td>-0.131319</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-31</th>\n",
       "      <td>3307.1700</td>\n",
       "      <td>3103.6400</td>\n",
       "      <td>0.065578</td>\n",
       "      <td>0.063517</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-12-31</th>\n",
       "      <td>2493.9000</td>\n",
       "      <td>3307.1700</td>\n",
       "      <td>-0.245911</td>\n",
       "      <td>-0.282245</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-12-31</th>\n",
       "      <td>3050.1200</td>\n",
       "      <td>2493.9000</td>\n",
       "      <td>0.223032</td>\n",
       "      <td>0.201333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-12-31</th>\n",
       "      <td>3473.0700</td>\n",
       "      <td>3050.1200</td>\n",
       "      <td>0.138667</td>\n",
       "      <td>0.129858</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-12-31</th>\n",
       "      <td>3639.7800</td>\n",
       "      <td>3473.0700</td>\n",
       "      <td>0.048001</td>\n",
       "      <td>0.046884</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-31</th>\n",
       "      <td>3089.2579</td>\n",
       "      <td>3639.7800</td>\n",
       "      <td>-0.151251</td>\n",
       "      <td>-0.163992</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-12-31</th>\n",
       "      <td>2974.9348</td>\n",
       "      <td>3089.2579</td>\n",
       "      <td>-0.037007</td>\n",
       "      <td>-0.037709</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-31</th>\n",
       "      <td>3351.7630</td>\n",
       "      <td>2974.9348</td>\n",
       "      <td>0.126668</td>\n",
       "      <td>0.119264</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-12-31</th>\n",
       "      <td>3250.6010</td>\n",
       "      <td>3351.7630</td>\n",
       "      <td>-0.030182</td>\n",
       "      <td>-0.030647</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                Close   Preclose  Raw_return  Log_return\n",
       "Day                                                     \n",
       "2002-12-31  1357.6500        NaN         NaN         NaN\n",
       "2003-12-31  1497.0400  1357.6500    0.102670    0.097735\n",
       "2004-12-31  1266.5000  1497.0400   -0.153997   -0.167233\n",
       "2005-12-31  1161.0600  1266.5000   -0.083253   -0.086924\n",
       "2006-12-31  2675.4700  1161.0600    1.304334    0.834792\n",
       "2007-12-31  5261.5600  2675.4700    0.966593    0.676302\n",
       "2008-12-31  1820.8100  5261.5600   -0.653941   -1.061146\n",
       "2009-12-31  3277.1400  1820.8100    0.799825    0.587690\n",
       "2010-12-31  2808.0800  3277.1400   -0.143131   -0.154470\n",
       "2011-12-31  2199.4200  2808.0800   -0.216753   -0.244307\n",
       "2012-12-31  2269.1300  2199.4200    0.031695    0.031203\n",
       "2013-12-31  2115.9800  2269.1300   -0.067493   -0.069878\n",
       "2014-12-31  3234.6800  2115.9800    0.528691    0.424412\n",
       "2015-12-31  3539.1800  3234.6800    0.094136    0.089965\n",
       "2016-12-31  3103.6400  3539.1800   -0.123062   -0.131319\n",
       "2017-12-31  3307.1700  3103.6400    0.065578    0.063517\n",
       "2018-12-31  2493.9000  3307.1700   -0.245911   -0.282245\n",
       "2019-12-31  3050.1200  2493.9000    0.223032    0.201333\n",
       "2020-12-31  3473.0700  3050.1200    0.138667    0.129858\n",
       "2021-12-31  3639.7800  3473.0700    0.048001    0.046884\n",
       "2022-12-31  3089.2579  3639.7800   -0.151251   -0.163992\n",
       "2023-12-31  2974.9348  3089.2579   -0.037007   -0.037709\n",
       "2024-12-31  3351.7630  2974.9348    0.126668    0.119264\n",
       "2025-12-31  3250.6010  3351.7630   -0.030182   -0.030647"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "使用groupby计算的年度收益率:\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Raw_Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>year</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2002</th>\n",
       "      <td>-0.192575</td>\n",
       "      <td>-0.175167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2003</th>\n",
       "      <td>0.097735</td>\n",
       "      <td>0.102670</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004</th>\n",
       "      <td>-0.167233</td>\n",
       "      <td>-0.153997</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2005</th>\n",
       "      <td>-0.086924</td>\n",
       "      <td>-0.083253</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006</th>\n",
       "      <td>0.834792</td>\n",
       "      <td>1.304334</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2007</th>\n",
       "      <td>0.676302</td>\n",
       "      <td>0.966593</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008</th>\n",
       "      <td>-1.061146</td>\n",
       "      <td>-0.653941</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009</th>\n",
       "      <td>0.587690</td>\n",
       "      <td>0.799825</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2010</th>\n",
       "      <td>-0.154470</td>\n",
       "      <td>-0.143131</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011</th>\n",
       "      <td>-0.244307</td>\n",
       "      <td>-0.216753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012</th>\n",
       "      <td>0.031203</td>\n",
       "      <td>0.031695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013</th>\n",
       "      <td>-0.069878</td>\n",
       "      <td>-0.067493</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014</th>\n",
       "      <td>0.424412</td>\n",
       "      <td>0.528691</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015</th>\n",
       "      <td>0.089965</td>\n",
       "      <td>0.094136</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016</th>\n",
       "      <td>-0.131319</td>\n",
       "      <td>-0.123062</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017</th>\n",
       "      <td>0.063517</td>\n",
       "      <td>0.065578</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018</th>\n",
       "      <td>-0.282245</td>\n",
       "      <td>-0.245911</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019</th>\n",
       "      <td>0.201333</td>\n",
       "      <td>0.223032</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020</th>\n",
       "      <td>0.129858</td>\n",
       "      <td>0.138667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021</th>\n",
       "      <td>0.046884</td>\n",
       "      <td>0.048001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022</th>\n",
       "      <td>-0.163991</td>\n",
       "      <td>-0.151250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023</th>\n",
       "      <td>-0.037709</td>\n",
       "      <td>-0.037007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024</th>\n",
       "      <td>0.119264</td>\n",
       "      <td>0.126668</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025</th>\n",
       "      <td>-0.030647</td>\n",
       "      <td>-0.030182</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Log_return  Raw_Return\n",
       "year                        \n",
       "2002   -0.192575   -0.175167\n",
       "2003    0.097735    0.102670\n",
       "2004   -0.167233   -0.153997\n",
       "2005   -0.086924   -0.083253\n",
       "2006    0.834792    1.304334\n",
       "2007    0.676302    0.966593\n",
       "2008   -1.061146   -0.653941\n",
       "2009    0.587690    0.799825\n",
       "2010   -0.154470   -0.143131\n",
       "2011   -0.244307   -0.216753\n",
       "2012    0.031203    0.031695\n",
       "2013   -0.069878   -0.067493\n",
       "2014    0.424412    0.528691\n",
       "2015    0.089965    0.094136\n",
       "2016   -0.131319   -0.123062\n",
       "2017    0.063517    0.065578\n",
       "2018   -0.282245   -0.245911\n",
       "2019    0.201333    0.223032\n",
       "2020    0.129858    0.138667\n",
       "2021    0.046884    0.048001\n",
       "2022   -0.163991   -0.151250\n",
       "2023   -0.037709   -0.037007\n",
       "2024    0.119264    0.126668\n",
       "2025   -0.030647   -0.030182"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算年度收益率\n",
    "# 方法1：使用resample函数的'YE'参数（年末）\n",
    "Year_data1 = data_new.resample('Y')['Log_return'].sum().to_frame(name='Log_return')\n",
    "Year_data1['Raw_Return'] = np.exp(Year_data1['Log_return']) - 1\n",
    "\n",
    "# 方法2：使用年末价格计算\n",
    "Year_data2 = data_new.resample('Y')['Close'].last().to_frame()\n",
    "Year_data2['Preclose'] = Year_data2['Close'].shift(1)\n",
    "Year_data2['Raw_return'] = Year_data2['Close'] / Year_data2['Preclose'] - 1\n",
    "Year_data2['Log_return'] = np.log(Year_data2['Close']) - np.log(Year_data2['Preclose'])\n",
    "\n",
    "# 方法3：使用groupby按年分组\n",
    "data_new4 = data_new.copy()\n",
    "data_new4['year'] = data_new4.index.year\n",
    "Year_data3 = data_new4.groupby('year')['Log_return'].sum().to_frame()\n",
    "Year_data3['Raw_Return'] = np.exp(Year_data3['Log_return']) - 1\n",
    "\n",
    "# 显示结果\n",
    "print(\"年度对数收益率汇总:\")\n",
    "Year_data1\n",
    "print(\"\\n年末价格计算的收益率:\")\n",
    "Year_data2\n",
    "print(\"\\n使用groupby计算的年度收益率:\")\n",
    "Year_data3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63bbd72a",
   "metadata": {},
   "source": [
    "# 计算滚动收益率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "6d53c1d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "滚动收益率 (基于对数收益率累加):\n",
      "            Rolling_5d_Return  Rolling_10d_Return  Rolling_20d_Return  \\\n",
      "Day                                                                     \n",
      "2025-01-21           0.000519            0.004019           -0.032416   \n",
      "2025-01-22          -0.004181           -0.005122           -0.053014   \n",
      "2025-01-23          -0.001813            0.005845           -0.048090   \n",
      "2025-01-24           0.003333            0.026543           -0.042804   \n",
      "2025-01-27           0.001918            0.028425           -0.043981   \n",
      "\n",
      "            Rolling_30d_Return  Rolling_60d_Return  \n",
      "Day                                                 \n",
      "2025-01-21           -0.046998           -0.023953  \n",
      "2025-01-22           -0.061074           -0.022147  \n",
      "2025-01-23           -0.058944           -0.011045  \n",
      "2025-01-24           -0.060342           -0.008293  \n",
      "2025-01-27           -0.041652           -0.006544  \n",
      "\n",
      "滚动收益率 (基于价格变化):\n",
      "            Rolling_5d_Price_Return  Rolling_10d_Price_Return  \\\n",
      "Day                                                             \n",
      "2025-01-21                 0.000519                  0.004019   \n",
      "2025-01-22                -0.004181                 -0.005122   \n",
      "2025-01-23                -0.001813                  0.005845   \n",
      "2025-01-24                 0.003333                  0.026543   \n",
      "2025-01-27                 0.001918                  0.028425   \n",
      "\n",
      "            Rolling_20d_Price_Return  Rolling_30d_Price_Return  \\\n",
      "Day                                                              \n",
      "2025-01-21                 -0.032416                 -0.046998   \n",
      "2025-01-22                 -0.053014                 -0.061074   \n",
      "2025-01-23                 -0.048090                 -0.058944   \n",
      "2025-01-24                 -0.042804                 -0.060342   \n",
      "2025-01-27                 -0.043981                 -0.041652   \n",
      "\n",
      "            Rolling_60d_Price_Return  \n",
      "Day                                   \n",
      "2025-01-21                 -0.023953  \n",
      "2025-01-22                 -0.022147  \n",
      "2025-01-23                 -0.011045  \n",
      "2025-01-24                 -0.008293  \n",
      "2025-01-27                 -0.006544  \n"
     ]
    }
   ],
   "source": [
    "# 计算滚动收益率（例如：过去30天、60天、90天的收益率 注意这里指的是前30个观测值）\n",
    "# 这在金融分析中非常常见，用于观察不同时间窗口的收益表现\n",
    "\n",
    "# 方法1：使用rolling窗口函数计算滚动对数收益率之和\n",
    "rolling_returns = pd.DataFrame()\n",
    "for window in [5, 10, 20, 30, 60]:\n",
    "    # 计算滚动窗口的对数收益率之和\n",
    "    rolling_log_return = data_new['Log_return'].rolling(window=window).sum()\n",
    "    # 转换为原始收益率\n",
    "    rolling_returns[f'Rolling_{window}d_Return'] = np.exp(rolling_log_return) - 1\n",
    "\n",
    "# 方法2：使用pct_change计算滚动价格变化\n",
    "rolling_price_returns = pd.DataFrame()\n",
    "for window in [5, 10, 20, 30, 60]:\n",
    "    rolling_price_returns[f'Rolling_{window}d_Price_Return'] = data_new['Close'].pct_change(periods=window)\n",
    "\n",
    "# 显示结果\n",
    "print(\"滚动收益率 (基于对数收益率累加):\")\n",
    "print(rolling_returns.tail())\n",
    "print(\"\\n滚动收益率 (基于价格变化):\")\n",
    "print(rolling_price_returns.tail())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d6651c0f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不同方法计算的累积收益率:\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Cumulative_Log_Return</th>\n",
       "      <th>Cumulative_Return</th>\n",
       "      <th>Cumulative_Return_Prod</th>\n",
       "      <th>Cumulative_Return_Alt</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2002-01-04</th>\n",
       "      <td>-0.021233</td>\n",
       "      <td>-0.021009</td>\n",
       "      <td>-0.021009</td>\n",
       "      <td>-0.021009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-01-07</th>\n",
       "      <td>-0.030353</td>\n",
       "      <td>-0.029897</td>\n",
       "      <td>-0.029897</td>\n",
       "      <td>-0.029897</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-01-08</th>\n",
       "      <td>-0.038718</td>\n",
       "      <td>-0.037978</td>\n",
       "      <td>-0.037978</td>\n",
       "      <td>-0.037978</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-01-09</th>\n",
       "      <td>-0.052779</td>\n",
       "      <td>-0.051410</td>\n",
       "      <td>-0.051410</td>\n",
       "      <td>-0.051410</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-01-10</th>\n",
       "      <td>-0.043161</td>\n",
       "      <td>-0.042243</td>\n",
       "      <td>-0.042243</td>\n",
       "      <td>-0.042243</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-21</th>\n",
       "      <td>0.678054</td>\n",
       "      <td>0.970040</td>\n",
       "      <td>0.970040</td>\n",
       "      <td>0.970040</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-22</th>\n",
       "      <td>0.669071</td>\n",
       "      <td>0.952422</td>\n",
       "      <td>0.952422</td>\n",
       "      <td>0.952422</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-23</th>\n",
       "      <td>0.674204</td>\n",
       "      <td>0.962471</td>\n",
       "      <td>0.962471</td>\n",
       "      <td>0.962471</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-24</th>\n",
       "      <td>0.681134</td>\n",
       "      <td>0.976118</td>\n",
       "      <td>0.976118</td>\n",
       "      <td>0.976118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-01-27</th>\n",
       "      <td>0.680511</td>\n",
       "      <td>0.974887</td>\n",
       "      <td>0.974887</td>\n",
       "      <td>0.974887</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5597 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Cumulative_Log_Return  Cumulative_Return  Cumulative_Return_Prod  \\\n",
       "Day                                                                            \n",
       "2002-01-04              -0.021233          -0.021009               -0.021009   \n",
       "2002-01-07              -0.030353          -0.029897               -0.029897   \n",
       "2002-01-08              -0.038718          -0.037978               -0.037978   \n",
       "2002-01-09              -0.052779          -0.051410               -0.051410   \n",
       "2002-01-10              -0.043161          -0.042243               -0.042243   \n",
       "...                           ...                ...                     ...   \n",
       "2025-01-21               0.678054           0.970040                0.970040   \n",
       "2025-01-22               0.669071           0.952422                0.952422   \n",
       "2025-01-23               0.674204           0.962471                0.962471   \n",
       "2025-01-24               0.681134           0.976118                0.976118   \n",
       "2025-01-27               0.680511           0.974887                0.974887   \n",
       "\n",
       "            Cumulative_Return_Alt  \n",
       "Day                                \n",
       "2002-01-04              -0.021009  \n",
       "2002-01-07              -0.029897  \n",
       "2002-01-08              -0.037978  \n",
       "2002-01-09              -0.051410  \n",
       "2002-01-10              -0.042243  \n",
       "...                           ...  \n",
       "2025-01-21               0.970040  \n",
       "2025-01-22               0.952422  \n",
       "2025-01-23               0.962471  \n",
       "2025-01-24               0.976118  \n",
       "2025-01-27               0.974887  \n",
       "\n",
       "[5597 rows x 4 columns]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算累积收益率\n",
    "# 累积收益率用于观察长期投资表现，从某个起始点开始累积\n",
    "\n",
    "# 方法1：使用对数收益率累加后转换\n",
    "# 这是最准确的方法，特别是对于长期累积\n",
    "cumulative_returns = pd.DataFrame()\n",
    "cumulative_returns['Cumulative_Log_Return'] = data_new['Log_return'].cumsum()\n",
    "cumulative_returns['Cumulative_Return'] = np.exp(cumulative_returns['Cumulative_Log_Return']) - 1\n",
    "\n",
    "# 方法2：使用cumprod函数直接累乘(1+r)\n",
    "# 这种方法在金融实践中也很常见\n",
    "cumulative_returns['Cumulative_Return_Prod'] = (1 + data_new['Raw_return']).cumprod() - 1\n",
    "\n",
    "# 方法3：使用pandas的累积函数\n",
    "cumulative_returns['Cumulative_Return_Alt'] = data_new['Raw_return'].add(1).cumprod().sub(1)\n",
    "\n",
    "# 显示结果\n",
    "print(\"不同方法计算的累积收益率:\")\n",
    "cumulative_returns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7833aacd",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
